﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BotonJXC.BLL;
using BotonJXC.DAO;
using BotonJXC.Entity;
using BotonJXC.Util;
using NHibernate;
using ComponentFactory.Krypton.Toolkit;

namespace BotonJXC.Forms.Controls
{
    public partial class RoleControl : BaseInfoControl
    {
        IList<Role> _roleDatas = null;
        RoleBLL _roleBll = new RoleBLL();
        string _roleName;
        bool _enabledEdit = true;
        /// <summary>
        /// 在页码改变时发生
        /// </summary>
        public event EventHandler PageChanged = null;

        public event EventHandler SelectedRow = null;

        public RoleControl()
        {
            InitializeComponent();
        }

        public bool EnabledEdit
        {
            get { return _enabledEdit; }
            set { _enabledEdit = value; }
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvRole.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _roleName = ktbRoleName.Text;
            pager1.PageNo = 1;
            LoadData();
        }

        private void RoleControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                if (!_enabledEdit)
                {
                    kdgvRole.ReadOnly = true;
                    kdgvRole.AllowUserToAddRows = false;
                }
                LoadData();
            }
        }

        private void LoadData()
        {
            PageModel<Role> pageModel = _roleBll.Find(_roleName, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            _roleDatas = pageModel.Results;
            kdgvRole.DataSource = new BindingList<Role>(_roleDatas);
        }

        private void kdgvRole_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (!DesignMode)
            {
                if (e.ColumnIndex == 1 && _roleDatas.Count > e.RowIndex)
                {
                    Role role = _roleDatas[e.RowIndex];
                    e.Value = "Y".Equals(role.Enabled) ? "是" : "否";
                }
            }
        }

        public override void AddInfo()
        {

        }
        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvRole.Rows)
                {
                    Role u = r.DataBoundItem as Role;
                    if (u == null)
                        continue;
                    if (u.RoleId == 0)
                    {
                        _roleBll.Save(u);
                    }
                    else if ("1".Equals(r.Tag) && u.RoleId != 0)
                    {
                        _roleBll.Update(u);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public override void DeleteInfo()
        {
            IList<Role> roles = (IList<Role>)kdgvRole.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvRole.SelectedRows)
                {
                    Role item = r.DataBoundItem as Role;
                    if (item != null)
                    {
                        _roleBll.Delete(item);
                    }
                    roles.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvRole.Refresh();
        }

        private void kdgvRole_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvRole.Rows[e.RowIndex].Tag = "1";
        }

        private void kdgvRole_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (kdgvRole.CurrentCell.ColumnIndex == 1)
            {
                KryptonDataGridViewComboBoxEditingControl cbc = e.Control as KryptonDataGridViewComboBoxEditingControl;
                cbc.DropDownClosed += new EventHandler(cbc_DropDownClosed);
            }
        }

        void cbc_DropDownClosed(object sender, EventArgs e)
        {
            KryptonDataGridViewComboBoxEditingControl cbc = sender as KryptonDataGridViewComboBoxEditingControl;
            IList<Role> itemDatas = (IList<Role>)kdgvRole.DataSource;
            itemDatas[kdgvRole.CurrentCell.RowIndex].Enabled = cbc.SelectedIndex == 0 ? "Y" : "N";
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
            if (PageChanged != null)
                PageChanged(sender, e);
        }

        private void kdgvRole_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (SelectedRow != null)
                SelectedRow(sender, EventArgs.Empty);
        }
    }
}
